package com.huike.clues.service.impl;

import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.service.ClueService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author STJ12138
 * @version 1.0
 * @description: TODO
 * @date 2023/7/12 11:19
 */
@Service
public class ClueServiceImpl implements ClueService {
    @Resource
    private TbClueMapper clueMapper;

    @Override
    public List<TbClueTrackRecord> list(Long clueId) {
        List<TbClueTrackRecord> list = clueMapper.list(clueId);
        return list;
    }

    @Override
    public TbClueTrackRecord getById(Long id) {
        return clueMapper.getById(id);
    }

    @Transactional
    @Override
    public Long add(ClueTrackRecordVo clueTrackRecordVo) {
        TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
        BeanUtils.copyProperties(clueTrackRecordVo,tbClueTrackRecord);
        tbClueTrackRecord.setCreateBy(SecurityUtils.getUsername());
        //先添加跟进数据，并得到添加后的id
        clueMapper.insertClueTrackRecord(tbClueTrackRecord);
        Long id = tbClueTrackRecord.getId();

        TbClue tbClue = new TbClue();
        BeanUtils.copyProperties(clueTrackRecordVo,tbClue);
        tbClue.setId(clueTrackRecordVo.getClueId());
        tbClue.setUpdateTime(DateUtils.getNowDate());
        //更新跟进线索的数据
        clueMapper.update(tbClue);
        return id;
    }
}
